import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/ext/screen_ext.dart';
import 'package:winkai/res/app_colors.dart';

void showOptionDialog(
  BuildContext context, {
  required List<String> options,
  Function(String name, int index)? onOptionClickListener,
}) {
  showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      builder: (context) {
        return OptionDialog(
          options: options,
          onOptionClickListener: onOptionClickListener,
        );
      });
}

class OptionDialog extends StatefulWidget {
  const OptionDialog({
    super.key,
    required this.options,
    this.onOptionClickListener,
  });

  final List<String> options;
  final Function(String name, int index)? onOptionClickListener;

  @override
  State<StatefulWidget> createState() {
    return _OptionDialogState();
  }
}

class _OptionDialogState extends State<OptionDialog> {
  @override
  Widget build(BuildContext context) {
    var options = widget.options;
    return Padding(
      padding: EdgeInsets.only(
          left: 16.w, right: 16.w, bottom: navigationHeight + 10.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16), color: Colors.white),
            clipBehavior: Clip.antiAlias,
            child: ListView.separated(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                var item = options[index];
                return _buildNormalItem(item, onTap: () {
                  Get.back();
                  widget.onOptionClickListener?.call(item, index);
                });
              },
              separatorBuilder: (context, index) {
                return Divider(
                  height: 0.5.w,
                  thickness: 0.5.w,
                  color: const Color(0xffcdcdcd),
                );
              },
              itemCount: options.length,
            ),
          ),
          SizedBox(height: 8.w),
          _buildSingleItem(
            'cancel'.tr,
            onTap: () {
              Get.back();
            },
          )
        ],
      ),
    );
  }

  Widget _buildNormalItem(String title, {GestureTapCallback? onTap}) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.translucent,
      child: Container(
        height: 46.w,
        padding: EdgeInsets.only(
          left: 15.w,
          right: 15.w,
        ),
        color: AppColors.color_f7,
        alignment: Alignment.center,
        child: Text(
          title,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            fontSize: 16.sp,
            color: AppColors.color_4d,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  Widget _buildSingleItem(String title, {GestureTapCallback? onTap}) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.translucent,
      child: Container(
        height: 52.w,
        padding: EdgeInsets.only(
          left: 15.w,
          right: 15.w,
        ),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16),
          color: AppColors.color_f7,
        ),
        child: Text(
          title,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            fontSize: 16.sp,
            color: AppColors.color_4d,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }
}
